// 关于用户操作的接口API函数
import request, { baseURL } from '@/utils/request'
import axios from 'axios'

// 用户登陆
export const userLogin = ({ mobile, code }) => {
  return request(
    {
      url: '/v1_0/authorizations',
      method: 'POST',
      data: { mobile, code }
    })
}

// 发送验证码
export const sendMessage = (mobile) => {
  return request({
    url: '/v1_0/sms/codes/' + mobile
  })
}

// 关注/取消作者
export const followAuthor = (authorId, isFollow) => {
  if (isFollow) {
    // 关注作者
    return request({
      url: '/v1_0/user/followings',
      method: 'POST',
      data: { target: authorId }
    })
  } else {
    // 取消关注作者
    return request({
      url: '/v1_0/user/followings/' + authorId,
      method: 'DELETE'
    })
  }
}

// 获取用户个人资料
export const getUserInfo = () => {
  return request({
    url: '/v1_0/user'
  })
}

// 获取用户自己信息
export const getUserProfile = () => {
  return request({
    url: '/v1_0/user/profile'
  })
}

// 更换用户图片
export const updateUserPhoto = (fromData) => {
  return request({
    url: '/v1_0/user/photo',
    method: 'PATCH',
    data: fromData
  })
}

// 更新用户信息
export const updateUserProfile = (user) => {
  return request({
    url: '/v1_0/user/profile',
    method: 'PATCH',
    data: user
  })
}

// 刷新token函数
export const refreshTokenAPI = (refreshtoken) => {
  return axios({
    url: baseURL + '/v1_0/authorizations',
    method: 'PUT',
    headers: {
      Authorization: `Bearer ${refreshtoken}`
    }
  })
}
